package com.example.myapplication.slice;

import com.example.baselib.TitleLayout;
import com.example.baselib.base.BaseAbilitySlice;
import com.example.baselib.base.BaseFraction;
import com.example.baselib.utils.UIUtils;
import com.example.baselib.utils.log.LogUtils;
import com.example.myapplication.ResourceTable;
import com.example.myapplication.fraction.MainFraction;
import com.example.myapplication.fraction.MyFraction;
import ohos.aafwk.ability.fraction.Fraction;
import ohos.aafwk.ability.fraction.FractionAbility;
import ohos.aafwk.ability.fraction.FractionManager;
import ohos.aafwk.ability.fraction.FractionScheduler;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.Image;
import ohos.agp.components.Text;
import ohos.agp.utils.Color;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;


public class MainAbilitySlice extends BaseAbilitySlice {

    List<? super BaseFraction> fractionList;

    private BaseFraction mLastFraction;
    BaseFraction mCurrFraction = null;

    @Override
    protected int initResLayoutId() {
        return ResourceTable.Layout_ability_main;
    }


    private void fractionSwitch(int index) {
        String tag = ResourceTable.Id_content_main + "_" + index;
        FractionManager manager = ((FractionAbility) getAbility()).getFractionManager();
        FractionScheduler fractionScheduler = manager.startFractionScheduler();
        Optional<Fraction> fraction = manager.getFractionByTag(tag);
        if (!fraction.isPresent()) {
            try {
                mCurrFraction = (BaseFraction) fractionList.get(index);
                fractionScheduler.add(ResourceTable.Id_content_main, mCurrFraction, tag);
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            mCurrFraction = (BaseFraction) fraction.get();
        }
        if (mCurrFraction == null) {
            return;
        }
        if (mLastFraction != null) {
            fractionScheduler.hide(mLastFraction);
        }
        fractionScheduler.show(mCurrFraction);
        fractionScheduler.submit();
        this.mLastFraction = mCurrFraction;
    }


    protected void initComponent() {
        fractionList = new ArrayList<>();
        fractionList.add(new MainFraction(this));
        fractionList.add(new MyFraction(this));

        fractionSwitch(0);

        TitleLayout layout = UIUtils.findComponentFromId(this, ResourceTable.Id_title1);
        layout.setTitle(ResourceTable.String_title_main_ability);

        DirectionalLayout mainLayout = UIUtils.findComponentFromId(this, ResourceTable.Id_main_layout);
        DirectionalLayout myLayout = UIUtils.findComponentFromId(this, ResourceTable.Id_my_layout);

        initSelectedStyle(mainLayout, ResourceTable.Media_icon_main_selected);
        initNormalStyle(myLayout, ResourceTable.Media_icon_my);

        mainLayout.setClickedListener(component -> {
            initSelectedStyle(mainLayout, ResourceTable.Media_icon_main_selected);
            initNormalStyle(myLayout, ResourceTable.Media_icon_my);
            fractionSwitch(0);
        });
        myLayout.setClickedListener(component -> {
            initSelectedStyle(myLayout, ResourceTable.Media_icon_my_selected);
            initNormalStyle(mainLayout, ResourceTable.Media_icon_main);
            fractionSwitch(1);
        });
    }

    private void initNormalStyle(DirectionalLayout layout, int iconResId) {
        Component component;

        for (int i = 0; i < layout.getChildCount(); i++) {
            component = layout.getComponentAt(i);
            if (component instanceof Image) {
                ((Image) component).setPixelMap(iconResId);
            }
            if (component instanceof Text) {
                ((Text) component).setTextColor(new Color(Color.rgb(139, 139, 139)));
                ((Text) component).setTextSize(12, Text.TextSizeType.FP);
            }
        }
    }


    private void initSelectedStyle(DirectionalLayout layout, int iconResId) {
        Component component;

        for (int i = 0; i < layout.getChildCount(); i++) {
            component = layout.getComponentAt(i);
            if (component instanceof Image) {
                ((Image) component).setPixelMap(iconResId);
            }
            if (component instanceof Text) {
                ((Text) component).setTextColor(new Color(Color.rgb(12, 96, 189)));
                ((Text) component).setTextSize(12, Text.TextSizeType.FP);
            }

        }
    }

    @Override
    protected void onAbilityResult(int requestCode, int resultCode, Intent resultData) {
        //拦截处理
        if (mCurrFraction != null && mCurrFraction.onAbilityResultIntercept(requestCode, resultCode, resultData)) {
            LogUtils.i("返回事件已拦截,requestCode:%s", requestCode);
            return;
        }
        super.onAbilityResult(requestCode, resultCode, resultData);
    }
}
